package cn.lngaosu.lwyd.module.system.controller.admin.webinterfacelog;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.lngaosu.lwyd.framework.common.pojo.PageParam;
import cn.lngaosu.lwyd.framework.common.pojo.PageResult;
import cn.lngaosu.lwyd.framework.common.pojo.CommonResult;
import cn.lngaosu.lwyd.framework.common.util.object.BeanUtils;
import static cn.lngaosu.lwyd.framework.common.pojo.CommonResult.success;

import cn.lngaosu.lwyd.framework.excel.core.util.ExcelUtils;

import cn.lngaosu.lwyd.framework.apilog.core.annotation.ApiAccessLog;
import static cn.lngaosu.lwyd.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.lngaosu.lwyd.module.system.controller.admin.webinterfacelog.vo.*;
import cn.lngaosu.lwyd.module.system.dal.dataobject.webinterfacelog.WebinterfaceLogDO;
import cn.lngaosu.lwyd.module.system.service.webinterfacelog.WebinterfaceLogService;

@Tag(name = "管理后台 - 接口数据-请求日志")
@RestController
@RequestMapping("/system/webinterface-log")
@Validated
public class WebinterfaceLogController {

    @Resource
    private WebinterfaceLogService webinterfaceLogService;

    @PostMapping("/create")
    @Operation(summary = "创建接口数据-请求日志")
    @PreAuthorize("@ss.hasPermission('system:webinterface-log:create')")
    public CommonResult<Long> createWebinterfaceLog(@Valid @RequestBody WebinterfaceLogSaveReqVO createReqVO) {
        return success(webinterfaceLogService.createWebinterfaceLog(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新接口数据-请求日志")
    @PreAuthorize("@ss.hasPermission('system:webinterface-log:update')")
    public CommonResult<Boolean> updateWebinterfaceLog(@Valid @RequestBody WebinterfaceLogSaveReqVO updateReqVO) {
        webinterfaceLogService.updateWebinterfaceLog(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除接口数据-请求日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:webinterface-log:delete')")
    public CommonResult<Boolean> deleteWebinterfaceLog(@RequestParam("id") Long id) {
        webinterfaceLogService.deleteWebinterfaceLog(id);
        return success(true);
    }

    @GetMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除接口数据-请求日志")
                @PreAuthorize("@ss.hasPermission('system:webinterface-log:delete')")
    public CommonResult<Boolean> deleteWebinterfaceLogList(@RequestParam("ids") List<Long> ids) {
        webinterfaceLogService.deleteWebinterfaceLogListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得接口数据-请求日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:webinterface-log:query')")
    public CommonResult<WebinterfaceLogRespVO> getWebinterfaceLog(@RequestParam("id") Long id) {
        WebinterfaceLogDO webinterfaceLog = webinterfaceLogService.getWebinterfaceLog(id);
        return success(BeanUtils.toBean(webinterfaceLog, WebinterfaceLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得接口数据-请求日志分页")
    @PreAuthorize("@ss.hasPermission('system:webinterface-log:query')")
    public CommonResult<PageResult<WebinterfaceLogRespVO>> getWebinterfaceLogPage(@Valid WebinterfaceLogPageReqVO pageReqVO) {
        PageResult<WebinterfaceLogDO> pageResult = webinterfaceLogService.getWebinterfaceLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WebinterfaceLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出接口数据-请求日志 Excel")
    @PreAuthorize("@ss.hasPermission('system:webinterface-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWebinterfaceLogExcel(@Valid WebinterfaceLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WebinterfaceLogDO> list = webinterfaceLogService.getWebinterfaceLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "接口数据-请求日志.xls", "数据", WebinterfaceLogRespVO.class,
                        BeanUtils.toBean(list, WebinterfaceLogRespVO.class));
    }

}